/**
 * @author vahid
 */
function FindTagsByName(container, name, Tag){
    var elements = document.getElementsByTagName(Tag);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].getAttribute("name") == name) {
            container.push(elements[i]);
        }
    }
}

function isValidDate(data){
    var gd = new Date(data);
    
    var hrs = gd.getHours();
    if (isNaN(hrs)) 
        return false;
    
    var mins = gd.getMinutes();
    if (isNaN(mins)) 
        return false;
    
    return true;
}

function syntaxHL(){
    var elements = [];
    FindTagsByName(elements, "code", "pre");
    FindTagsByName(elements, "code", "textarea");
    
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].nodeName.toUpperCase() == "TEXTAREA") {
            var childNode = elements[i].childNodes[0];
            var newNode = document.createTextNode(childNode.nodeValue.replace(/<br\s*\/?>/gi, '\n'));
            elements[i].replaceChild(newNode, childNode);
            
        }
        else 
            if (elements[i].nodeName.toUpperCase() == "PRE") {
                brs = elements[i].getElementsByTagName("br");
                for (var j = 0, brLength = brs.length; j < brLength; j++) {
                    var newNode = document.createTextNode("\n");
                    elements[i].replaceChild(newNode, brs[0]);
                }
            }
    }
    
    //clipboard does not work well, no line breaks
    // dp.SyntaxHighlighter.ClipboardSwf = 
    //"http://vahid.nasiri.googlepages.com/clipboard.swf";
    dp.SyntaxHighlighter.HighlightAll("code");
}

//farsi-date mod. for blogger by vahid-nasiri ...
$(document).ready(function(){
    $("h2.date-header").each(function(){
        var obj = $(this);
        if (isValidDate(obj.text())) 
            obj.html(ToPersianDate(new Date(obj.text())));
    });
    
    $("span[class='post-date']").each(function(){
        var obj = $(this);
        if (isValidDate(obj.text())) 
            obj.html(ToPersianDate(new Date(obj.text())));
    });
    
    $("a[title='comment permalink']").each(function(){
        var obj = $(this);
        obj.html(ToPersianDateLong(obj.text()));
    });
    
    $("p[class='comment permalink']").each(function(){
        var obj = $(this);
        obj.html(ToPersianDateLong(obj.text()));
    });
    
    $("a.post-count-link").each(function(){
        var obj = $(this);
        obj.html(getBloggerPMonthNames(obj.text()));
    });
    
    $("a.post-count-link").each(function(){
        var obj = $(this);
        obj.html(getBloggerPYear(obj.text()));
    });
    
    //fav-icons
    $("a").each(function(){
        var $a = $(this);
        var href = $a.attr("href");
        // see if the link is external  
        if (href) 
            if (href != 'undefined' && href != 'http://feeds2.feedburner.com/vahidnasiri') 
                if (href.match(/^http/)) 
                    if (!href.match(document.domain)) {
                        var domain = href.replace(/<\S[^><]*>/g, "").split('/')[2];
                        var image = '<img src="http://' + domain + '/favicon.ico" width="0" onload="this.width=11;this.height=10;this.style.paddingLeft=\'3px\';this.style.paddingRight=\'1px\';" style="border:0" onerror="this.src=\'http://vahidnasiri.persiangig.com/document/weblink.gif\';" />';
                        $(this).prepend(image);
                    }
    });
    
    //google-analytics
    $("a").click(function(){
        var $a = $(this);
        var href = $a.attr("href");
        
        // see if the link is external  
        if (href) 
            if (href != 'undefined') 
                if ((href.match(/^http/)) && (!href.match(document.domain))) {
                    // if so, register an event  
                    var category = "outgoing"; // set this to whatever you want  
                    var event = "click"; // set this to whatever you want  
                    var label = href; // set this to whatever you want  
                    pageTracker._trackPageview("/outgoing/" + href);
                    pageTracker._trackEvent(category, event, href);
                }
    });
    
    syntaxHL();
	
	$("a[href='http://vahid.nasiri.googlepages.com/cntme.htm?TB_iframe=true&height=340&width=530']").each(function(){
	  var obj = $(this);
	  obj.attr("title","Contact me");
	  obj.attr("class","thickbox");
	}).bind("contextmenu",function(e){
        return false;
    });

});

